package com.tour.controller;

import com.tour.bean.BaseParam;
import com.tour.bean.ReturnT;
import com.tour.bean.Ticketinfo;
import com.tour.service.TicketinfoService;
import com.tour.util.Util;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

/**
 * @ClassName TicketinfoManageController
 * @Author 张梦兴
 * @Date 2020/3/17 22:24
 * @describe 票务后台管理
 */
@RestController
@RequestMapping("admin")
public class TicketinfoManageController {

    @Autowired
    private TicketinfoService ticketinfoService;

    /**
     * [刪除]
     * @author
     * @date 2020/03/17
     **/
    @RequestMapping("/ticketManage/delete/{id}")
    public ReturnT<String> delete(@PathVariable("id") int id){
        return ticketinfoService.delete(id);
    }

    /**
     * [更新]
     * @author
     * @date 2020/03/17
     **/
    @RequestMapping("/ticketManage/update")
    public ReturnT<String> update(Ticketinfo ticketinfo){
        return ticketinfoService.update(ticketinfo);
    }

    @RequestMapping("/ticketManage/insert")
    public ReturnT<String> insert(BaseParam baseParam) throws Exception {
        Ticketinfo info=new Ticketinfo();
        JSONObject json = baseParam.getParamJson();
        String train=json.getString("train");
        String startSt=json.getString("startSt");
        String startTime=json.getString("startTime");
        String lastStat=json.getString("lastStat");
        String lastTime=json.getString("lastTime");

        info.setTrainInfo(train);
        info.setStartStation(startSt);
        info.setStartTime(Util.getDateByString(startTime, "yyyy-MM-dd HH:mm:ss"));
        info.setLastStation(lastStat);
        info.setLastTime(Util.getDateByString(lastTime, "yyyy-MM-dd HH:mm:ss"));

        info.setRunTime(Util.getDateDiffer(startTime,lastTime));
        return ticketinfoService.insert(info);
    }
}
